import {
  _path,
  Write_player,
  existplayer,
  bound,
  Write_cangku
} from '../../mode/allapi'
import { plugin, AMessage } from '../../api/api'

export class Up extends plugin {
  constructor() {
    super({
      dsc: '战斗',
      rule: [
        {
          reg: /^(#|\/)?出战(.*)*$/,
          fnc: 'playing'
        },
        {
          reg: /^(#|\/)?离阵.*$/,
          fnc: 'ending'
        },
        {
          reg: /^(#|\/)?令.*领兵.*$/,
          fnc: 'setting'
        }
      ]
    })
  }
  async playing(e: AMessage) {
    //判断存档
    const id = e.user_id
    if (!(await existplayer(id))) return false
    const didian = e.msg.replace(/(#|\/)?出战/, '')
    const [thing_data, thing_shu] = didian.split('*')
    //读取兵营
    const binyin = await (await _path(id)).bingyin
    //读取阵法
    const player = await (await _path(id)).player
    if (player.state == 1) return e.reply('行军中')
    if (player.garrison == 1) return e.reply(`军队驻守中`)
    const player_wujian = []
    for (let i = 0; i < player.wujian.length; i++) {
      if (player.wujian[i].name != undefined) {
        player_wujian.push(player.wujian[i])
      }
    }
    if (player_wujian.length >= 5) return e.reply('军师:阵法最多上阵五名将领')
    if (thing_data == '士兵') {
      return e.reply('军师：主公不可只上阵士兵')
    }
    const obj = await bound(thing_data, binyin, 'name')
    if (obj == undefined) return e.reply(`军师：主公还未有此将领`)
    else if (
      (parseInt(thing_shu) <= 0 && parseInt(thing_shu) >= 10) ||
      thing_shu == null
    )
      return false
    const index = binyin.indexOf(obj)
    if (player.wujian[parseInt(thing_shu) - 1].name != undefined) {
      return e.reply(`军师：此位置已有其他将领`)
    }
    if (player.wujian.some(warrior => warrior.name == thing_data)) {
      return e.reply(`军师：阵中已有同名将领`)
    }

    if (obj.type == '武将' || obj.type == '谋士') {
      const newobj = JSON.parse(JSON.stringify(obj))
      if (obj.num > 1) {
        binyin[index].num -= 1
        newobj.num = 1
      } else {
        binyin.splice(index, 1)
      }
      player.wujian[parseInt(thing_shu) - 1] = newobj
    }
    Write_player(id, player)
    Write_cangku('barracks', id, binyin)
    e.reply(`军师：${thing_data}入阵，定能大破敌军`)
  }

  async ending(e: AMessage) {
    const id = e.user_id
    if ((await existplayer(id)) != true) return false
    const binyin = await (await _path(id)).bingyin
    const player = await (await _path(id)).player
    if (player.state == 1) return e.reply('行军中')
    if (player.garrisons == 1) return e.reply(`军队驻守中`)
    const didian = e.msg.replace(/(#|\/)?离阵/, '')
    const obj1 = await bound(didian, player.wujian, 'name')
    if (obj1 == undefined) {
      e.reply(`军师：${didian}并未上阵`)
      return
    }
    const index1 = player.wujian.indexOf(obj1) 
    let bing = await bound('士兵', binyin, 'name')
    if (bing == undefined) {
      bing = {
        id: 1000,
        name: '士兵',
        type: '士兵',
        space: 1,
        price: 100,
        num: 0
      }
      binyin.push(bing) 
    }
    if (obj1 != undefined) {
      bing.num += player.wujian[index1].hp
      player.wujian[index1].hp = 0
      player.wujian[index1] = {}
      binyin.push(obj1)
    }
    await Write_player(id, player)
    await Write_cangku('barracks', id, binyin)
    e.reply(`${didian}：收兵，回营`)
  }

  async setting(e: AMessage) {
    const id = e.user_id
    if (e.isGroup != true) return false
    if ((await existplayer(id)) != true) return false
    const binyin = await (await _path(id)).bingyin
    const player = await (await _path(id)).player
    if (player.state == 1) return e.reply('行军中')
    if (player.garrison == 1) return e.reply(`军队驻守中`)
    const match = e.msg.match(/令(.*)领兵(\d+)/)
    const wujianName = match[1]
    const playerIndex = player.wujian.findIndex(
      element => element.name === wujianName
    )
    if (playerIndex === -1) {
      return await e.reply('该武将未在阵上')
    }
    const shibin = await bound('士兵', binyin, 'name')
    if (
      player.wujian[playerIndex].hp + Number(match[2]) >
      player.wujian[playerIndex].maxHp
    ) {
      return await e.reply(
        `此武将尚且能力不足，最多可领兵${player.wujian[playerIndex].maxHp}`
      )
    }
    if (shibin == undefined || shibin.num < match[2])
      return e.reply('兵营士兵数量不足')
    shibin.num -= parseInt(match[2])
    player.wujian[playerIndex].hp += Number(match[2])
    await Write_cangku('barracks', id, binyin)
    await Write_player(id, player)
    e.reply(`配置完成，现该武将配置有${player.wujian[playerIndex].hp}士兵`)
  }
}
